package it.gestoreContratti.inputVerifiers;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class DateInputVerifier extends InputVerifier {

	private String field;
	
	public DateInputVerifier(String field) {
		this.field = field;			
	}
	
	@Override
	public boolean verify(JComponent input) {
		JTextField textField = (JTextField) input;
		if(textField.getText().length() != 0){
			try{
				DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
				dateFormat.parse(textField.getText());				
				return true;						
			} catch (ParseException e) {
				JOptionPane.showMessageDialog(null, "Il campo "+field+" deve essere una data nel formato GG/MM/AAAA", "", JOptionPane.WARNING_MESSAGE);
				textField.setText("");
				textField.validate();
				return false;
			}			
		}		
		return true;
	}

}
